

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:projectfluttershengyihuatong/category/catetory_controller.dart';
import 'CatetoryModel.dart';
import 'package:projectfluttershengyihuatong/tool/public_color.dart';
import 'package:projectfluttershengyihuatong/tool/extension_int.dart';
class CategoryCell extends StatelessWidget  {

  const CategoryCell(
      {super.key, required this.model, required this.streamController});

  final CatetoryModel model;
  final StreamController<CatetoryModel> streamController;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        streamController.add(model);
        CatetoryController.getInstance().menModel = model;
      },
      key: ValueKey(model.title),
      child:StreamBuilder(
        stream: streamController.stream,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            //判断内存地址是否是一样的。
            if (identical(snapshot.data, model)) {
              model.isSelected = true;
            } else {
              model.isSelected = false;
            }
          }
          final title = model.title ?? "";
          final isSelected = model.isSelected;

          return CellContainer(isSelected: isSelected, title: title);
        },
      ),
    );

  }
}

class CellContainer extends StatelessWidget {
  const CellContainer({
    super.key,
    required this.isSelected,
    required this.title,
  });

  final bool isSelected;
  final String title;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: null,
      color: isSelected ? Colors.white : colorf9,
      padding: const EdgeInsets.fromLTRB(10, 12, 10, 12),
      child: Center(
        child: Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(
                width: 2,
                height: 10,
                child: ColoredBox(
                  color: isSelected ?
                  primaryColor
                      : Colors.transparent,
                ),
              ),
              const SizedBox(
                width: 8,
              ),
              Text(
                title,
                style: TextStyle(
                  fontSize: 17.adapt(),
                  color: isSelected ? primaryColor : color33,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}